/*
 *  This file is part of ZiggyStardust.
 *
 *  ZiggyStardust is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  ZiggyStardust is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.wisc.csvParser.plugins;

import java.awt.FileDialog;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import java.io.File;


/**
 *
 * @author  lawinslow
 */
public class JPanelGLEONCentral extends javax.swing.JPanel {
    
    private DataParserGLEONCentral parser;
    /** Creates new form JPanelGLEONCentral
     * @param parser The underlying data parser object.
     */
    public JPanelGLEONCentral(DataParserGLEONCentral parser) {
        initComponents();
        
        this.parser = parser;
        
        parser.addEventHandler(new DataParserGLEONCentral.IGLEONCentralHandler(){
            @Override
            public void eventRaised(DataParserGLEONCentral.events e){
                handleEvent(e);
            }
        });

        updateDisplay();
        
    }
    
    public void handleEvent(DataParserGLEONCentral.events e){
        
        if(e == DataParserGLEONCentral.events.stopped ||
                e == DataParserGLEONCentral.events.started){
            
            startedCB.setSelected(parser.isStarted());
            //UI is disabled when started
            monitoredPathTB.setEnabled(!parser.isStarted());
            getPathButton.setEnabled(!parser.isStarted());
            
        }else if(e == DataParserGLEONCentral.events.valParsed){
            parsedValuesLabel.setText("Parsed Values:"
                    +Long.toString(parser.getCountParsed()));
        }else if(e == DataParserGLEONCentral.events.reconfigured){
            monitoredPathTB.setText(parser.getMonitoredPath());
        }
    }

    private void updateDisplay(){
        startedCB.setSelected(parser.isStarted());
        //UI is disabled when started
        monitoredPathTB.setEnabled(!parser.isStarted());
        getPathButton.setEnabled(!parser.isStarted());
        parsedValuesLabel.setText("Parsed Values:"
                +Long.toString(parser.getCountParsed()));
        monitoredPathTB.setText(parser.getMonitoredPath());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        monitoredPathTB = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        startedCB = new javax.swing.JCheckBox();
        parsedValuesLabel = new javax.swing.JLabel();
        getPathButton = new javax.swing.JButton();

        jLabel1.setText("Monitored Path:");

        startedCB.setText("Started");
        startedCB.setEnabled(false);

        parsedValuesLabel.setText("Parsed Values: 0");

        getPathButton.setText("....");
        getPathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getPathButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(monitoredPathTB, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(getPathButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(41, 41, 41)
                .addComponent(startedCB)
                .addContainerGap(34, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(parsedValuesLabel)
                .addContainerGap(314, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(monitoredPathTB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(getPathButton)))
                    .addComponent(startedCB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(parsedValuesLabel)
                .addContainerGap(175, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void getPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_getPathButtonActionPerformed
        JFileChooser newPath = new JFileChooser();
        newPath.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        
       // FileDialog newPath = new FileDialog(
        //        (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this));
        newPath.showOpenDialog(
                (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this));
        
        if(newPath.getSelectedFile()!=null){
            if(newPath.getSelectedFile().isDirectory()){
                parser.setMonitoredPath(newPath.getSelectedFile().getPath());
            }
        }
    }//GEN-LAST:event_getPathButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton getPathButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField monitoredPathTB;
    private javax.swing.JLabel parsedValuesLabel;
    private javax.swing.JCheckBox startedCB;
    // End of variables declaration//GEN-END:variables
    
}
